<template>
  <div class="student-statistics">
    <el-row :gutter="20">
      <!-- 统计卡片 -->
      <el-col :span="6" v-for="card in statisticsCards" :key="card.title">
        <el-card class="statistics-card" :body-style="{ padding: '20px' }">
          <div class="card-content">
            <div class="card-value">
              <span class="number">{{ card.value }}</span>
              <span class="unit">{{ card.unit }}</span>
            </div>
            <div class="card-title">{{ card.title }}</div>
            <div class="card-trend">
              <span :class="['trend', card.trend > 0 ? 'up' : 'down']">
                {{ Math.abs(card.trend) }}%
                <el-icon>
                  <CaretTop v-if="card.trend > 0" />
                  <CaretBottom v-else />
                </el-icon>
              </span>
              <span class="trend-text">较上月</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 考试通过率趋势图 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>考试通过率趋势</span>
              <el-radio-group v-model="trendTimeRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="year">本年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div class="chart-container" ref="trendChartRef"></div>
        </el-card>
      </el-col>

      <!-- 考试成绩分布图 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>考试成绩分布</span>
              <el-select v-model="distributionSubject" size="small">
                <el-option label="科目一" value="subject1" />
                <el-option label="科目二" value="subject2" />
                <el-option label="科目三" value="subject3" />
                <el-option label="科目四" value="subject4" />
              </el-select>
            </div>
          </template>
          <div class="chart-container" ref="distributionChartRef"></div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 学员成绩排名 -->
      <el-col :span="24">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>学员成绩排名</span>
              <div class="header-operations">
                <el-select v-model="rankingSubject" style="width: 120px; margin-right: 16px;" size="small">
                  <el-option label="科目一" value="subject1" />
                  <el-option label="科目二" value="subject2" />
                  <el-option label="科目三" value="subject3" />
                  <el-option label="科目四" value="subject4" />
                </el-select>
                <el-select v-model="rankingTimeRange" size="small">
                  <el-option label="本周" value="week" />
                  <el-option label="本月" value="month" />
                  <el-option label="本年" value="year" />
                </el-select>
              </div>
            </div>
          </template>
          <el-table
            :data="rankingData"
            style="width: 100%"
            :max-height="400"
          >
            <el-table-column type="index" label="排名" width="80" />
            <el-table-column prop="name" label="学员姓名" width="120" />
            <el-table-column prop="score" label="考试成绩" width="100">
              <template #default="{ row }">
                <span :class="getScoreClass(row.score)">{{ row.score }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="examDate" label="考试日期" width="120" />
            <el-table-column prop="examCount" label="考试次数" width="100" />
            <el-table-column prop="coachName" label="教练姓名" width="120" />
            <el-table-column prop="remark" label="备注" min-width="200" show-overflow-tooltip />
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { CaretTop, CaretBottom } from '@element-plus/icons-vue'
import * as echarts from 'echarts'

// 统计卡片数据
const statisticsCards = [
  {
    title: '今日考试',
    value: 28,
    unit: '人次',
    trend: 12.5
  },
  {
    title: '通过率',
    value: 92.5,
    unit: '%',
    trend: 3.2
  },
  {
    title: '平均分',
    value: 88.6,
    unit: '分',
    trend: 1.5
  },
  {
    title: '补考率',
    value: 15.8,
    unit: '%',
    trend: -2.3
  }
]

// 图表相关
const trendTimeRange = ref('month')
const distributionSubject = ref('subject2')
const rankingSubject = ref('subject2')
const rankingTimeRange = ref('month')

const trendChartRef = ref(null)
const distributionChartRef = ref(null)

let trendChart = null
let distributionChart = null

// 排名数据
const rankingData = ref([
  {
    name: '张三',
    score: 98,
    examDate: '2024-03-15',
    examCount: 1,
    coachName: '李教练',
    remark: '表现优秀，一次性通过'
  },
  {
    name: '李四',
    score: 95,
    examDate: '2024-03-15',
    examCount: 1,
    coachName: '王教练',
    remark: '操作稳定，发挥正常'
  },
  {
    name: '王五',
    score: 92,
    examDate: '2024-03-14',
    examCount: 2,
    coachName: '李教练',
    remark: '第二次考试通过'
  },
  {
    name: '赵六',
    score: 88,
    examDate: '2024-03-14',
    examCount: 1,
    coachName: '张教练',
    remark: '基本功扎实'
  }
])

// 获取成绩样式
const getScoreClass = (score) => {
  if (score >= 95) return 'score-excellent'
  if (score >= 90) return 'score-good'
  if (score >= 80) return 'score-normal'
  return 'score-poor'
}

// 初始化趋势图
const initTrendChart = () => {
  trendChart = echarts.init(trendChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['科目一', '科目二', '科目三', '科目四']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: {
      type: 'value',
      name: '通过率(%)',
      min: 0,
      max: 100
    },
    series: [
      {
        name: '科目一',
        type: 'line',
        smooth: true,
        data: [95, 93, 96, 94, 95, 92, 94]
      },
      {
        name: '科目二',
        type: 'line',
        smooth: true,
        data: [85, 88, 82, 86, 85, 89, 87]
      },
      {
        name: '科目三',
        type: 'line',
        smooth: true,
        data: [82, 85, 80, 83, 86, 84, 85]
      },
      {
        name: '科目四',
        type: 'line',
        smooth: true,
        data: [92, 90, 93, 91, 94, 92, 93]
      }
    ]
  }
  trendChart.setOption(option)
}

// 初始化分布图
const initDistributionChart = () => {
  distributionChart = echarts.init(distributionChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '成绩分布',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 35, name: '95分以上' },
          { value: 45, name: '90-95分' },
          { value: 15, name: '80-90分' },
          { value: 5, name: '80分以下' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  distributionChart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  trendChart?.resize()
  distributionChart?.resize()
}

onMounted(() => {
  // 初始化图表
  initTrendChart()
  initDistributionChart()

  // 添加窗口大小变化监听
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  // 移除窗口大小变化监听
  window.removeEventListener('resize', handleResize)

  // 销毁图表实例
  trendChart?.dispose()
  distributionChart?.dispose()
})
</script>

<style scoped>
.student-statistics {
  padding: 20px;
}

.statistics-card {
  margin-bottom: 20px;
}

.card-content {
  text-align: center;
}

.card-value {
  margin-bottom: 8px;
}

.card-value .number {
  font-size: 24px;
  font-weight: bold;
  margin-right: 4px;
}

.card-value .unit {
  font-size: 14px;
  color: #909399;
}

.card-title {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.card-trend {
  font-size: 12px;
}

.trend {
  display: inline-flex;
  align-items: center;
  margin-right: 4px;
}

.trend.up {
  color: #67c23a;
}

.trend.down {
  color: #f56c6c;
}

.trend-text {
  color: #909399;
}

.chart-row {
  margin-bottom: 20px;
}

.chart-card {
  height: 400px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-operations {
  display: flex;
  align-items: center;
}

.chart-container {
  height: 320px;
}

.score-excellent {
  color: #67c23a;
  font-weight: bold;
}

.score-good {
  color: #409eff;
  font-weight: bold;
}

.score-normal {
  color: #e6a23c;
}

.score-poor {
  color: #f56c6c;
}
</style>